//
//  LoginViewController.m
//  SAILIN
//
//  Created by 崔国冠 on 2018/10/19.
//  Copyright © 2018年 赛麟. All rights reserved.
//

#import "LoginViewController.h"
#import "MainViewController.h"
#import "RegisterViewController.h"
#import "PhoneShortcutLoginViewController.h"
#import "NIAlertViewController.h"
#import "LoginViewModel.h"
@interface LoginViewController ()

@property(nonatomic,strong) LoginViewModel *loginViewModel;

@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;

@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@end

@implementation LoginViewController

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
     _loginViewModel = [LoginViewModel new];
    
    [self addRACRegister];
   
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self initUI];
    
}
#pragma mark - UI

- (void)initUI{
    
    [self.userNameTextField addTarget:self  action:@selector(textDidChange:)  forControlEvents:UIControlEventAllEditingEvents];
    [self.passwordTextField addTarget:self  action:@selector(textDidChange:)  forControlEvents:UIControlEventAllEditingEvents];
    
    self.userNameTextField.limitMaxTextLength = 11;
    self.passwordTextField.limitMaxTextLength = 20;
    
    [self setRightBtn1WithTitle:@"注册" backgroundImageName:nil action:@selector(registerAction) target:self];
    [self hiddenRightBtn2];
    [self setBackBtnHidden:YES];
}

#pragma mark - Action
#pragma mark 登录
- (IBAction)loginAction:(id)sender {
    
    if (![MXValidate validateMobile:self.userNameTextField.text]) {
        [self showToast:AlertTextPhoneError];
        return;
    }
    if (self.passwordTextField.text.length == 0) {
        [self showToast:AlertTextPasswordIsNull];
        return;
    }
    if (![MXValidate validatePassword:self.passwordTextField.text]) {
        [self showToast:AlertTextPasswordError];
        return;
    }
    
    [NILoadingView show:LoadingTextLogin];
    
    [self.loginViewModel loginWithPhone:self.userNameTextField.text
                               password:self.passwordTextField.text];
    
}
#pragma mark 注册
- (void)registerAction{
    
    [self toRegisterViewController];
}
#pragma mark 快捷登录
- (IBAction)phoneShortcutLoginAction:(id)sender {
    
    [self toPhoneShortcutLoginViewController];
    
}
#pragma mark 游客体验
- (IBAction)touristAction:(id)sender {
    
    [_loginViewModel clearData4loginDemo];
    
    NSString *message = @"您将以游客身份登录（安防密码为任意6位数）";
    NIAlertViewController *c = [NIAlertViewController alertControllerWithTitle:message message:nil preferredStyle:UIAlertControllerStyleAlert];
    [c setImageAndTitleFont:nil titleFont:UIFontPingFangRegular(17) messageFont:nil];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        //demo模式自动登录
        [NILoadingView show:LoadingTextLogin];
        
        [self.loginViewModel loginByDemo];
        
    }];
    [c addAction:cancelAction];
    [self presentViewController:c animated:YES completion:^{
    }];
    
}

#pragma mark - textFieldDelegate
- (void)textDidChange:(id)sender {
    self.loginButton.enabled = self.userNameTextField.text.length > 0 && self.passwordTextField.text.length > 0;
}

#pragma mark - RAC
-(void)addRACRegister
{
    [RACObserve(self.loginViewModel, loginCode) subscribeNext:^(id  _Nullable x) {
        switch ([x integerValue]) {
            case NIRequestStatusSuccess:
                NSLog(@"登录成功");
                [NILoadingView close];
                
                [self toMainViewController];
                
                break;
            case NIRequestStatusFailed:

                break;
                
            default:
                break;
        }
    }];
}
#pragma mark - 跳转
- (void)toMainViewController{
    
    UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    MainViewController *VC = [mainSB instantiateViewControllerWithIdentifier:@"MainViewController"];
    [self.navigationController pushViewController:VC animated:YES];
    
}
- (void)toRegisterViewController{
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    RegisterViewController *VC = [storyboard instantiateViewControllerWithIdentifier:@"RegisterViewController"];
    [self.navigationController pushViewController:VC animated:YES];
    
}

- (void)toPhoneShortcutLoginViewController{
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    PhoneShortcutLoginViewController *VC = [storyboard instantiateViewControllerWithIdentifier:@"PhoneShortcutLoginViewController"];
    [self.navigationController pushViewController:VC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
